var sp_shareUIs;
var EndPageLayer = cc.Layer.extend({
	sprite:null,
	btn_Replay:null,
	btn_Upload:null,
	txt_Money:null,
	txt_Word:null,
	txt_Rank:null,
	pic_Endback:null,
	pic_EndDown:null,
	endpagelayer:null,
	ctor:function () {
		//////////////////////////////
		// 1. super init first
		this._super();
		endpagelayer=this;
		/////////////////////////////
		// 2. add a menu item with "X" image, which is clicked to quit the program
		//    you may modify it.
		// ask the window size
		var size = cc.winSize;
		// add a "close" icon to exit the progress. it's an autorelease object
		var startScene=ccs.load(res.endscene_json).node;
		this.addChild(startScene);

		pic_Endback=startScene.getChildByName("Image_2");
		pic_EndDown=startScene.getChildByName("Image_1");
		pic_Endback.setScaleY(theScaleH);
		
		txt_Money=startScene.getChildByName("txt_1");
		txt_Word=startScene.getChildByName("Text_1");
//		txt_Rank=startScene.getChildByName("Text_2");
		txt_Word.setVisible(false);
//		txt_Rank.setVisible(false);
		
		
		txt_Money.setString(g_Value_gameScore);
		btn_Replay=ccui.helper.seekWidgetByName(startScene, "Button_1");
		btn_Upload=ccui.helper.seekWidgetByName(startScene, "Button_2");
		btn_Replay.addTouchEventListener(this.backClick);
		btn_Upload.setVisible(false);
		btn_Upload.addTouchEventListener(this.shareButtonClick);
		
		pic_EndDown.y+=(size.height-1024)*0.5;
		txt_Money.y+=(size.height-1024)*0.5;
		txt_Word.y+=(size.height-1024)*0.5;
//		txt_Rank.y+=(size.height-1024)*0.5;
		btn_Replay.y+=(size.height-1024)*0.5;
		btn_Upload.y+=(size.height-1024)*0.5;
		//return true;
		if(g_Value_UserRank==-1){
			insertDatas(g_String_serNmae,g_Value_gameScore,g_String_openId,g_String_headUrl);
			this.showWords();
		}else{
			if(g_Value_gameScore>g_Array_Users[g_Value_UserRank-1][2]){
				insertDatasTo(g_String_serNmae,g_Value_gameScore,g_String_openId,g_String_headUrl);
				this.showWords();
			}
		}

		txt_Word.setString("恭喜你，刷新了个人最佳成绩。\n全网排名第  "+g_Value_UserRank+"  名，快分享到朋友圈炫耀一下吧");
		
		//share(g_Value_gameScore);
		return true;
	},
	//分享按钮事件
	shareButtonClick:function(sender,type)
	{
		

		switch (type) {        
		case ccui.Widget.TOUCH_ENDED:
			//cc.director.runScene(new StartPageScene());
			//upLoadScore(g_Value_gameScore);
			//cc.log("shareButtonClick");
			sp_shareUIs = new ShareUI();
			//helloworldl.addChild(layers.shareUI, 100);
			endpagelayer.addChild(sp_shareUIs);
			share(2,g_Value_UserRank,g_Value_HighSocre);
			break;        
		default:  break;
		}

	},
	showWords:function(){
		for (var i = 0; i < g_Array_Users.length; i++) {
			if (g_Array_Users[i][2]< g_Value_gameScore) {
				g_Value_UserRank=i+1;
				break;
			}
		}
		txt_Word.setVisible(true);
		//txt_Rank.setVisible(true);
		btn_Upload.setVisible(true);
		//txt_Rank.setString(g_Value_UserRank);
	},
	backClick2:function(sender,type){
		switch (type) {        
		case ccui.Widget.TOUCH_ENDED:
			//cc.director.runScene(new StartPageScene());
			//upLoadScore(g_Value_gameScore);
			break;        
		default:  break;
		}
	},
	backClick:function(sender,type)
	{
		switch (type) {        
		case ccui.Widget.TOUCH_ENDED:
			cc.director.runScene(new StartPageScene());
			break;        
		default:  break;
		}
	}
});
var layers = {};
var ShareUI = cc.LayerColor.extend({
	ctor: function () {
		this._super(cc.color(0, 0, 0, 188), cc.winSize.width, cc.winSize.height);

		var arrow = new cc.Sprite(res.arrow);
		arrow.anchorX = 1;
		arrow.anchorY = 1;
		arrow.x = cc.winSize.width - 15;
		arrow.y = cc.winSize.height - 5;
		arrow.setScale(2.3, 2.3);
		this.addChild(arrow);

		var label = new cc.LabelTTF("请点击右上角的菜单按钮\n再点\"分享到朋友圈\"\n让好友们挑战你的分数！", "宋体", 35, cc.size(cc.winSize.width*0.7, 250), cc.TEXT_ALIGNMENT_CENTER);
		label.x = cc.winSize.width/2;
		label.y = cc.winSize.height - 200;
		label.anchorY = 1;
		this.addChild(label);
	},
	onEnter: function () {
		this._super();
		cc.eventManager.addListener({
			event: cc.EventListener.TOUCH_ONE_BY_ONE,
			onTouchBegan: function (touch, event) {
				cc.eventManager.removeListener(this);
				//cc.log("onTouchBegan")
//				sp_shareUIs.setVisible(false);
				sp_shareUIs.removeFromParent();
				//gameOverLayout.initButtonEvent();
				return false;
			}
		}, this);
	}
});
var EndPageScene = cc.Scene.extend({
	onEnter:function () {
		this._super();
		var layer = new EndPageLayer();
		this.addChild(layer);
	}
});

